﻿using System;

namespace Engine.Physic
{
    public class Ground
    {
        #region Public Fields

        public readonly byte[] Unknown;
        public short noCollision;

        #endregion Public Fields

        #region Private Fields

        private readonly char[] groundName;
        private readonly char[] temp;
        private byte[] unknown2;

        #endregion Private Fields

        #region Public Constructors

        public Ground()
        {
            this.groundName = new char[32];
            this.temp = new char[32];
            this.Unknown = new byte[64];
            this.unknown2 = new byte[14];
        }

        public Ground(string groundName, short frontGrip, short rearGrip, short Stickiness, short Dirtiness, EnumSkidmarks skidmarks, EnumNoise noise, bool NoCollision)
            : this()
        {
            groundName.CopyTo(0, this.groundName, 0, Math.Min(groundName.Length, 32));
            this.FrontGrip = frontGrip;
            this.RearGrip = rearGrip;
            this.Stickiness = Stickiness;
            this.Dirtiness = Dirtiness;
            this.Skidmarks = skidmarks;
            this.Noise = noise;
            this.NoCollision = NoCollision;
        }

        #endregion Public Constructors

        #region Public Properties

        public short Dirtiness { get; set; }

        public short FrontGrip { get; set; }

        public string Name
        {
            get { var text = new string(this.groundName).Trim().Replace("\0", ""); return text; }
            set { value.CopyTo(0, this.groundName, 0, Math.Max(32, this.groundName.Length)); }
        }

        public bool NoCollision { get; set; }
        public EnumNoise Noise { get; set; }
        public short RearGrip { get; set; }
        public EnumSkidmarks Skidmarks { get; set; }
        public short Stickiness { get; set; }

        #endregion Public Properties

        #region Public Methods

        public static Ground FromStream(System.IO.BinaryReader br)
        {
            Ground ground = new Ground();

            //   ground.groundName =
            br.ReadChars(32).CopyTo(ground.groundName, 0);// ObjectName.FromStream(br);

            //ground.temp = ObjectName.FromStream(br); //Only to add more 32 Bytes;
            br.ReadChars(32).CopyTo(ground.temp, 0);// ObjectName.FromStream(br);

            ground.Dirtiness = br.ReadInt16();
            ground.FrontGrip = br.ReadInt16();
            ground.RearGrip = br.ReadInt16();
            ground.Stickiness = br.ReadInt16();
            ground.Noise = (EnumNoise)br.ReadInt16();
            ground.Skidmarks = (EnumSkidmarks)br.ReadInt16();
            // ground.Unknown =//
            br.ReadBytes(64).CopyTo(ground.Unknown, 0);
            ground.noCollision = br.ReadInt16();
            ground.unknown2 = br.ReadBytes(14);

            return ground;
        }

        public void ToStream(System.IO.BinaryWriter bw)
        {
            bw.Write(this.groundName);
            bw.Write(this.temp);
            // this.groundName.Save(bw);
            //this.temp.Save(bw);
            bw.Write((short)this.Dirtiness);
            bw.Write((short)this.FrontGrip);
            bw.Write((short)this.RearGrip);
            bw.Write((short)this.Stickiness);
            bw.Write((short)this.Noise);
            bw.Write((short)this.Skidmarks);
            bw.Write(this.Unknown);
            bw.Write((short)this.noCollision);
            bw.Write(this.unknown2);
        }

        public override string ToString()
        {
            return this.Name;
        }

        #endregion Public Methods
    }
}